package com.github.beansoft.jadx.gui.idea.ui; /**
 * $ $ License.
 * <p>
 * Copyright $ L2FProd.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import com.intellij.openapi.actionSystem.ActionButtonComponent;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.ex.ActionButtonLook;
import com.intellij.util.ui.JBDimension;
import com.intellij.util.ui.JBInsets;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.StartupUiUtil;

import java.awt.*;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;

/**
 * BlueishButtonUI. <br>
 * Some UI taken from com.intellij.openapi.actionSystem.impl.ActionButton
 * @author beansoft
 * @date 2021-10-22
 */
public class ActionButtonUI
        extends BasicButtonUI {

    private static Color blueishBackgroundOver = new Color(224, 232, 246);
    private static Color blueishBorderOver = new Color(152, 180, 226);

    private static Color blueishBackgroundSelected = new Color(193, 210, 238);
    private static Color blueishBorderSelected = new Color(49, 106, 197);

    JBDimension myMinimumButtonSize = JBDimension.create(ActionToolbar.DEFAULT_MINIMUM_BUTTON_SIZE);

    public ActionButtonUI() {
        super();
    }

    /**
     * @see BasicButtonUI#installDefaults(AbstractButton)
     * @param c
     */
    public void installUI(JComponent c) {
        super.installUI(c);

//        button.setRolloverEnabled(true);
//        button.setPreferredSize(JBUI.size(32, 32));
//        button.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
        if(c instanceof JToggleButton) {
            LookAndFeel.uninstallBorder(c); // L&F 默认给ToggleButton添加了一个圆形border, 需要去掉 参考 installDefaults
        }
    }

    @Override
    public Dimension getPreferredSize( JComponent c ) {
        AbstractButton button = (AbstractButton) c;
        Icon icon = button.getIcon();

//        if (myMinimumButtonSize != null) myMinimumButtonSize.update();
        if(icon == null) {
            Dimension size = JBDimension.create(ActionToolbar.DEFAULT_MINIMUM_BUTTON_SIZE);
            JBInsets.addTo(myMinimumButtonSize, button.getInsets());
            return size;
        }
        JBInsets myInsets =  JBUI.emptyInsets();

        Dimension size = icon.getIconWidth() < myMinimumButtonSize.width && icon.getIconHeight() < myMinimumButtonSize.height ?
                new Dimension(myMinimumButtonSize) :
                new Dimension(Math.max(myMinimumButtonSize.width, icon.getIconWidth() + myInsets.left + myInsets.right),
                        Math.max(myMinimumButtonSize.height, icon.getIconHeight() + myInsets.top + myInsets.bottom));

        JBInsets.addTo(size, button.getInsets());
        return size;
    }

    /**
     * Copied from StartupUiUtil.isUnderDarcula()
     * @return
     */
    public static boolean isUnderDarcula() {
        return UIManager.getLookAndFeel().getName().contains("Darcula");
    }

    public void paint(Graphics g, JComponent c) {
        AbstractButton button = (AbstractButton) c;
        ActionButtonLook look = ActionButtonLook.SYSTEM_LOOK;

        if (button.getModel().isRollover()
                || button.getModel().isArmed()
                || button.getModel().isSelected()) {
//            Color oldColor = g.getColor();
//            if (button.getModel().isSelected()) {
//                g.setColor(blueishBackgroundSelected);
//            } else {
//                g.setColor(blueishBackgroundOver);
//            }
//            g.fillRect(0, 0, c.getWidth() - 1, c.getHeight() - 1);
//
//            if (button.getModel().isSelected()) {
//                g.setColor(blueishBorderSelected);
//            } else {
//                g.setColor(blueishBorderOver);
//            }
//            g.drawRect(0, 0, c.getWidth() - 1, c.getHeight() - 1);
//
//            g.setColor(oldColor);

            if (button.isEnabled()  || !isUnderDarcula() ) { // || !StartupUiUtil.isUnderDarcula()
                look.paintBackground(g, button, ActionButtonComponent.PUSHED);
            }

            paintIcon(g, button, button.getIcon());
            look.paintBorder(g, button, ActionButtonComponent.PUSHED);
        } else {
            // 未按下时
//            look.paintBackground(g, button, ActionButtonComponent.PUSHED);
            paintIcon(g, button, button.getIcon());
        }


//        if (button.getIcon() != null) {
//            this.paintIcon(g, c, iconRect);
//        }
//        super.paint(g, c);
    }

    public void paintIcon(Graphics g, JComponent actionButton, Icon icon) {
        Rectangle rect = new Rectangle(actionButton.getWidth(), actionButton.getHeight());
        Insets i = actionButton.getInsets();
        JBInsets.removeFrom(rect, i);

        int x = i.left + (rect.width - icon.getIconWidth()) / 2;
        int y = i.top + (rect.height - icon.getIconHeight()) / 2;
        paintIcon(g, actionButton, icon, x, y);
    }

    public void paintIcon(Graphics g, JComponent actionButton, Icon icon, int x, int y) {
        icon.paintIcon(actionButton instanceof Component ? (Component)actionButton : null, g, x, y);
    }
} 
